package com.ryong21.io.net;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Vector;

import org.sipdroid.codecs.Codec;
import org.sipdroid.codecs.Codecs;
import org.sipdroid.media.JAudioLauncher;
import org.sipdroid.media.MediaLauncher;
import org.sipdroid.net.RtpPacket;
import org.sipdroid.net.RtpSocket;
import org.sipdroid.net.SipdroidSocket;
import org.sipdroid.sipua.ui.Receiver;
import org.sipdroid.sipua.ui.Sipdroid;
import org.zoolu.sip.provider.SipStack;
import org.zoolu.tools.Log;
import org.zoolu.tools.RotatingLog;

import android.content.Context;

public class RTP {

	SipdroidSocket socket;
	RtpSocket rtp_socket;

	private Thread sender = new Thread() {
		public void run() {
			RtpPacket keepalive = new RtpPacket(new byte[12], 0);
			RtpPacket audioPacket = new RtpPacket(new byte[1000], 0);

			try {
				if (rtp_socket == null) {
					rtp_socket = new RtpSocket(socket = new SipdroidSocket(
							17778), InetAddress.getByName("10.1.1.102"), 17777);
					sleep(3000);
				} else{
					socket = rtp_socket.getDatagramSocket();
				}
				rtp_socket.getDatagramSocket().setSoTimeout(15000);
			} catch (Exception e) {
				if (!Sipdroid.release)
					e.printStackTrace();
				return;
			}
			keepalive.setPayloadType(126);
			try {
				rtp_socket.send(keepalive);
			} catch (Exception e1) {
				return;
			}
			for (;;) {
				try {
					rtp_socket.receive(audioPacket);
				} catch (IOException e) {
					rtp_socket.getDatagramSocket().disconnect();
					try {
						rtp_socket.send(keepalive);
					} catch (IOException e1) {
						return;
					}
				}
				if (audioPacket.getPayloadLength() > 200) {
					return;
				}
			}
		}
	};

	private MediaLauncher audio_app;
	private int local_audio_port = 17776;
	private int remote_audio_port = 17777;
	private String remote_media_address = "10.1.1.105";
	private String audio_in = "";
	private int dir = 0;
	private String audio_out = "";
	public void init(Context context){
		Log event_log = new RotatingLog("1_events.log", null,
				SipStack.debug_level, SipStack.max_logsize * 1024,
				SipStack.log_rotations, SipStack.rotation_scale,
				SipStack.rotation_time);
		Vector<Codec> codecmap = new Vector<Codec>(1);;
		Vector<Integer> numbers = new Vector<Integer>(1);
		numbers.add(0, 97);
		Codec codec = Codecs.getName("speex");
		codecmap.add(0,codec);
		Codecs.Map c = new Codecs.Map(0,codec, numbers, codecmap);
		Receiver.mContext = context;
		audio_app = new JAudioLauncher(local_audio_port,
				remote_media_address, remote_audio_port, dir, audio_in,
				audio_out, c.codec.samp_rate(),
				0,
				c.codec.frame_size(), event_log, c, 101);
		audio_app.startMedia();
	}
	public void writeTag(byte[] processed, int size, long ts) {
//		RtpPacket audioPacket = new RtpPacket(new byte[size], 0);
//		RtpPacket keepalive = new RtpPacket(new byte[12], 0);
//		keepalive.setPayloadType(126);
//		audioPacket.setTimestamp(ts);
//		audioPacket.setPayloadType(103);
//		try {
//			rtp_socket.send(audioPacket);
//		} catch (IOException e) {
//			rtp_socket.getDatagramSocket().disconnect();
//			try {
//				rtp_socket.send(keepalive);
//			} catch (IOException e1) {
//				return;
//			}
//		}
//		if (audioPacket.getPayloadLength() > 200) {
//			return;
//		}
	}

}
